/* $RCSFile: PageTreeDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/PageTreeDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;

using Telerik.Web.UI;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// Implements a DropDownList with all portal pages and allow selecting one
	/// </summary>
	public class PageTreeDataType : BaseDataType
	{
        private Unit _ComboHeight            =   new Unit("200px");

        
        public Unit ComboHeight
        {
            get
            { 
            return _ComboHeight;
            }
            set 
            { 
                _ComboHeight                    =   value;
                if (_IsInitialized)
                {
                    RadTreeComboPages RadTreeComboPages   =   (RadTreeComboPages)EditControl;
                    RadTreeComboPages.Height              =   value;
                }
            }
        }
        
       public override Unit controlWidth
        {
            get
            {
                return base.controlWidth;
            }
            set
            {
                base.controlWidth       =   value;
                if (_IsInitialized)
                {
                    RadTreeComboPages RadTreeComboPages     =   (RadTreeComboPages)EditControl;
                    RadTreeComboPages.Width       =   value;
                }
            }
        }

        bool _SelectFirst    =   true;
        /// <summary>
        /// Select the firts element in treeview if no selectedItem set
        /// Default True
        /// </summary>
        public bool SelectFirst
        {
            get
            {
                return _SelectFirst;
            }
            set
            {
                _SelectFirst = value;
                if (_IsInitialized)
                {
                    RadTreeComboPages RadTreeComboPages =   (RadTreeComboPages)EditControl;
                    RadTreeComboPages.SelectFirst       =   value;
                }
            }
        }

        bool _IncludeAdmin    =   false;
        /// <summary>
        /// Should we include the Admin elements (Pages or Modules) in the treeview
        /// Default False
        public bool IncludeAdmin
        {
            get
            {
                return _IncludeAdmin;
            }
            set
            {
                _IncludeAdmin = value;
                if (_IsInitialized)
                {
                    RadTreeComboPages RadTreeComboPages =   (RadTreeComboPages)EditControl;
                    RadTreeComboPages.IncludeAdmin       =   value;
                }
            }
        }

        string _SkipList    =   string.Empty;
        /// <summary>
        /// Must contain a list of Id to skip in the treeview, values separted by ';'
        /// Each page is referenced by is PageID
        /// Each module is referenced by is DefModuleID
        /// Default to string.Empty
        /// </summary>
        public string SkipList
        {
            get
            {
                return _SkipList;
            }
            set
            {
                _SkipList = value;
                if (_IsInitialized)
                {
                    RadTreeComboPages RadTreeComboPages =   (RadTreeComboPages)EditControl;
                    RadTreeComboPages.SkipList          =   value;
                }
            }
        }

        string _UnSelectables    =   string.Empty;
        /// <summary>
        /// Must contain a list of Id that would not be selectable in the treeview, values separted by ';'
        /// Default to string.Empty
        /// </summary>
        public string UnSelectables
        {
            get
            {
                return _UnSelectables;
            }
            set
            {
                _UnSelectables = value;
                if (_IsInitialized)
                {
                    RadTreeComboPages RadTreeComboPages =   (RadTreeComboPages)EditControl;
                    RadTreeComboPages.UnSelectables     =   value;
                }
            }
        }

         /// <summary>
		/// Ctor
		/// </summary>
		public PageTreeDataType() : base()
		{
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine("[V]PageTreeDataType Ctor BEG.", DataTypeTraceSwitch.Sw.Info);
			InnerDataType	    =   PropertiesDataType.Integer;
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine("[V]PageTreeDataType Ctor END.", DataTypeTraceSwitch.Sw.Info);
		}

		protected override void InitializeComponents()
		{
			if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageTreeDataType InitializeComponents BEG UseRadControl: {0}", _UsesRadControl),DataTypeTraceSwitch.Sw.Info);
			RadTreeComboPages rcp   =   new RadTreeComboPages();
            rcp.Width               =   controlWidth;
            rcp.Height              =   ComboHeight;
            rcp.SelectFirst         =   SelectFirst;
            rcp.IncludeAdmin        =   IncludeAdmin;
            rcp.SkipList            =   SkipList;
            rcp.UnSelectables       =   UnSelectables;
            if ( ReadOnly )
                rcp.Enabled         =   false;
			innerControl            =   rcp;
            _IsInitialized          =   true;
		}

        public override void DataBind()
        {
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]PageTreeDataType DataBind BEG.",DataTypeTraceSwitch.Sw.Info);
			try
			{
				RadTreeComboPages RadTreeComboPages     =   (RadTreeComboPages) EditControl;
                RadTreeComboPages.Width                 =   controlWidth;
                RadTreeComboPages.SelectedTreeViewValue =   Val;
                RadTreeComboPages.DataBind();
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]PageTreeDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]PageTreeDataType DataBind END.",DataTypeTraceSwitch.Sw.Info);
        }

        public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageTreeDataType Val SET BEG value: [{0}]", value),DataTypeTraceSwitch.Sw.Info);
				innerValue      = value;
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageTreeDataType Val SET innerControl is NULL: {0}", innerControl == null ),DataTypeTraceSwitch.Sw.Info);
                if ( innerControl == null )
                    return;
				//update value in control
   				DataBind();
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine("[V]PageTreeDataType Val SET END.",DataTypeTraceSwitch.Sw.Info);
			}
		}

        public override Control EditControl
		{
			get
			{
				if ( innerControl == null  )
					InitializeComponents();
				return innerControl;
			}
			set
			{
                if (DataTypeTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine("[V]PageTreeDataType EditControl SET BEG.", DataTypeTraceSwitch.Sw.Info);
				if (value is RadTreeComboPages)
				{
					innerControl                            =   value;
                    RadTreeComboPages RadTreeComboPages     =   (RadTreeComboPages)innerControl;
                    innerValue                              =   RadTreeComboPages.SelectedValue;
                    if (DataTypeTraceSwitch.Sw.TraceVerbose)
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageTreeDataType EditControl SET END innerValue: {0}.",innerValue), DataTypeTraceSwitch.Sw.Info);
				return;
				}
				string str	=	string.Format("[E]PageTreeDataType EditControl Set: a RadTreeComboPages value is required, a '{0}' is given.",value);
				if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine(str,DataTypeTraceSwitch.Sw.Info);
				throw new ArgumentException(str, "EditControl");
			}
		}

	}
}